
// UDI编码生成
// export const UDIRules = (codeString) => {
//     let s = codeString.split('').reverse().join('');

//     let sum = 0;
//     for (let i = 0; i < s.length; i++) {
//       sum += i % 2 === 0 ? +s[i] * 3 : +s[i];
//     }

//     return 10 - (sum % 10);
// }
export const UDIRules = (packageCode, itemCode, projectCode) => {
  //实例编码
  //let code = '0692048009419'

  //DI部分-缺校验位
  let code = packageCode + itemCode + projectCode;
  if (code.length != 13) {
    console.log('Code 长度错误')
    return ''
  }

  let array = [...code]
  console.log(array)

  //a.所有偶数位的数字代码求和
  //b.所有奇数位的数字代码求和
  let sum1 = 0
  let sum2 = 0
  for (let i = 0; i < array.length; i++) {
    if (i % 2 == 0) {
      sum1 += parseInt(array[i])
    }
    else {
      sum2 += parseInt(array[i])
    }
  }
  console.log(sum1)
  console.log(sum2)

  //将步骤a的和乘以3,再加上步骤b
  let sum3 = sum1 * 3 + sum2
  console.log(sum3)

  //用大于或等于步骤d所得结果且为10最小整数倍的数减去步骤d所得结果，其差即为所求校验码的值
  let dat = 0
  if (sum3 % 10 != 0) {
    dat = (parseInt(sum3 / 10) + 1) * 10
  }
  else {
    dat = sum3
  }
  console.log(dat)

  //求差值
  let diff = dat - sum3
  console.log(diff)

  //拼接结果
  let result = code + diff
  console.log(result)

  return diff;
}

// 使用EAN13条码格式
export const EAN13Rules = (codeString, verifyCode) => {
  return `${codeString}${verifyCode}`
}

// 使用GTIN14条码格式
export const GTIN14Rules = (codeString, packagingNumber, verifyCode) => {
  return `${packagingNumber}${codeString}${verifyCode}`
}